/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Br.Com.Cds.Academia.ContasPagar;

import Br.Com.Cds.Academia.Banco.BancoBean;
import Br.Com.Cds.Academia.DAO.AcessoMySQL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Pedro
 */
public class ContasPagarControl {
    PreparedStatement pstm;
    ResultSet rs;
    
    
    String consultacontaspagar = "SELECT * FROM CONTASPAGAR WHERE CONTA LIKE ?";
    String cadastracontaspagar = "INSERT INTO CONTASPAGAR (DATAEMISSAO,DATAVENCIMENTO,DESCRICAO,CONTA,TOTAL,PARCELAS,TITULO,SITUACAO)VALUES (?,?,?,?,?,?,?,?)";
    String alterarcontaspagar = "UPDATE CONTASPAGAR SET DATAEMISSAO=?,DATAVENCIMENTO=?,DESCRICAO=?, CONTA=?, TOTAL=?, PARCELAS=?, TITULO=?,SITUACAO=? WHERE IDCP=?";
    String excluircontaspagar = "DELETE FROM CONTASPAGAR WHERE IDCP=?"; 
    
    
    
    public void cadastraContasPagar (ContasPagarBean contaspagar){
        try {
            AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(cadastracontaspagar);
            pstm.setDate(1,contaspagar.getDataemissao());
            pstm.setDate(2, contaspagar.getDatavencimento());
            pstm.setString(3, contaspagar.getDescricao());
            pstm.setString(4, contaspagar.getConta());
            pstm.setString(5, contaspagar.getTotal());
            pstm.setString(6, contaspagar.getParcelas());
            pstm.setString(7, contaspagar.getTitulo());
            pstm.setString(8, contaspagar.getSituacao());
            pstm.executeUpdate();
            mysql.desconectar();
    
        } catch (SQLException e) {
            e.printStackTrace ();
            JOptionPane.showMessageDialog(null, "SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UM CONTAS A PAGAR!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE); 
        }
}


public void alterarContasPagar (ContasPagarBean contaspagar){
    
    try {
        AcessoMySQL mysql = new AcessoMySQL();
            pstm = (PreparedStatement) mysql.conectar().prepareStatement(alterarcontaspagar);
            pstm.setDate(1,contaspagar.getDataemissao());
            pstm.setDate(2, contaspagar.getDatavencimento());
            pstm.setString(3, contaspagar.getDescricao());
            pstm.setString(4, contaspagar.getConta());
            pstm.setString(5, contaspagar.getTotal());
            pstm.setString(6, contaspagar.getParcelas());
            pstm.setString(7, contaspagar.getTitulo());
            pstm.setString(8, contaspagar.getSituacao());
            pstm.setInt(9, contaspagar.getIdcp());
            pstm.executeUpdate();
            mysql.desconectar(); 
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,"SQL EXCEPTION ! ERRO AO TENTAR CADASTRAR UM UM CONTAS A PAGAR!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
    }
}

    
  public List <ContasPagarBean> listarContasPagar (String nomecontaspagar){
         List <ContasPagarBean> contaspagar = new ArrayList ();
         
         try {
             AcessoMySQL mysql = new AcessoMySQL();
             pstm = (PreparedStatement) mysql.conectar().prepareStatement(consultacontaspagar);
             pstm.setString(1, nomecontaspagar);
             rs = pstm.executeQuery();
             ContasPagarBean alu;
             
             while (rs.next()){
                alu = new ContasPagarBean ();
                alu.setIdcp(rs.getInt("IDCP"));          
                alu.setDataemissao(rs.getDate("DATAEMISSAO"));  
                alu.setDatavencimento(rs.getDate("DATAVENCIMENTO")); 
                alu.setDescricao(rs.getString("DESCRICAO")); 
                alu.setConta(rs.getString("CONTA"));  
                alu.setTotal(rs.getString("TOTAL"));   
                alu.setParcelas(rs.getString("PARCELAS"));   
                alu.setTitulo(rs.getString("TITULO"));   
                alu.setSituacao(rs.getString("SITUACAO"));   
                contaspagar.add(alu);  
             }    
      } catch (Exception e) {
        e.printStackTrace();
         JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR REALIZAR UMA CONSULTA","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
        
      }
        return contaspagar;
      
  }
    
    
  public void excluirContasPagar (int idcp){
      
      try {
          AcessoMySQL mysql = new AcessoMySQL();
          pstm = (PreparedStatement) mysql.conectar().prepareStatement(excluircontaspagar);
          pstm.setInt(1,idcp);
          pstm.executeUpdate();
          mysql.desconectar();
            
      } catch (Exception e) {
          e.printStackTrace();
          JOptionPane.showMessageDialog(null,"SQL EXCEPTION - ERRO AO TENTAR EXCLUIR UM CONTAS A PAGAR","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
      }
       
  }

    
    
    
    
    
}
